package team.sun.sss.controller.log;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import team.sun.sss.dao.log.BreakRuleDao;
import team.sun.sss.dao.log.LogCourseDao;
import team.sun.sss.dao.log.LogTalkDao;
import team.sun.sss.dao.log.NotifyDao;
import team.sun.sss.dao.staff.StaffDao;
import team.sun.sss.dao.student.StudentDao;
import team.sun.sss.pojo.student.Student1;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
public class logc {

    @Autowired
    LogTalkDao logTalkDao;

    @Autowired
    LogCourseDao logCourseDao;

    @Autowired
    BreakRuleDao breakRuleDao;

    @Autowired
    NotifyDao notifyDao;

    @Autowired
    StaffDao staffDao;

    @Autowired
    StudentDao studentDao;

    @GetMapping("/break_rule")
    public String break_rule(){
        return "log/break_rule";
    }

    @PostMapping("/log_add")
    public String log_add(Integer stu_id, String date, String log_place, String log_content, String log_describe, int userId){
        try {
            String sta_name= staffDao.getStaName(userId);
            logTalkDao.addLogTalk(stu_id,date,log_place,log_content,log_describe,sta_name);
        }catch (Exception e){
            System.out.println("添加失败");
            return "/log/error";
        }
        System.out.println("添加成功");
        return "/log/ins_log_work";
    }

    @PostMapping("/break_rule_add")
    public String break_rule_add(int stu_id, String bre_type, String bre_level, String bre_type_name, String bre_detail, String bre_time, String bre_relieve_time, String bre_operator){
        try {
            breakRuleDao.addBreakRule(stu_id,bre_type,bre_level,bre_type_name,bre_detail,bre_time,bre_relieve_time,bre_operator);
        }catch (Exception e){
            System.out.println(e);
            return "/log/error";
        }
        System.out.println("添加成功");
        List<Map<String ,Object>> s = studentDao.getUserList(stu_id);
        Map stu = s.get(0);
        try{
            notifyDao.addStuNotify(stu_id,"违纪处分公告","因为"+bre_detail+"给予"+bre_level+"处分处理","学工处");
            notifyDao.addIntNotify(staffDao.getStaIdOfName(stu.get("stu_instructor").toString()),"违纪处分公告",stu.get("stu_grade").toString()+"级"+stu.get("stu_college")+stu.get("stu_major")+stu.get("stu_class")+stu.get("stu_name")+"因为"+bre_detail+"给予"+bre_level+"处分处理","学工处");
        }catch (Exception e){
            System.out.println(e);
        }
        System.out.println("发送通告成功");
        return "log/break_rule";
    }

    @PostMapping("/log_course_add")
    public String log_course_add(String log_course_name, String log_course_sta, String log_course_major, String log_course_class, String log_place, String log_listen_date,String log_course_content, String log_describe, int userId){
        try {
            String log_sta = staffDao.getStaName(userId);
            Date date = new Date();
            logCourseDao.addLogCourse(log_course_name,log_course_sta,log_course_major,log_course_class,log_place,date.toLocaleString(),log_listen_date,log_course_content,log_describe,log_sta);
        }catch (Exception e){
            System.out.println(e);
            return "/log/error";
        }
        System.out.println("添加成功");
        return "log/ins_course_work";
    }

    @PostMapping("/logWork_update")
    public String logWork_update(int log_talk_id,int stu_id, String log_stu_date, String log_place, String log_content, String log_describe){
        try {
            logTalkDao.updateLogTalk(log_talk_id,stu_id,log_stu_date,log_place,log_content,log_describe);
        }catch (Exception e){
            System.out.println(e);
            return "/log/error";
        }
        System.out.println("修改成功");
        return "log/ins_log_work";
    }

    @PostMapping("/logCourse_update")
    public String logCourse_update(String log_course_name,String log_course_sta,String log_course_major,String log_course_class,String log_place,String log_listen_date,String log_course_content,String log_describe,int log_course_id){
        try {
            logCourseDao.updateCourse(log_course_name,log_course_sta,log_course_major,log_course_class,log_place,log_listen_date,log_describe,log_course_content,log_course_id);
        }catch (Exception e){
            System.out.println(e);
            return "/log/error";
        }
        System.out.println("修改成功");
        return "log/ins_course_work";
    }


    @PostMapping("/breakRule_update")
    public String breakRule_update(String bre_type,String bre_level,String bre_type_name,String bre_time,String bre_relieve_time,String bre_detail,int bre_id){
        try {
            breakRuleDao.updateBreak(bre_type,bre_level,bre_type_name,bre_time,bre_relieve_time,bre_detail,bre_id);
        }catch (Exception e){
            System.out.println(e);
            return "/log/error";
        }
        System.out.println("修改成功");
        return "log/break_rule";
    }


    @PostMapping("/notify")
    public String notify(int userType){
            switch(userType){
                case 3 :
                    return "log/notify";
                case 2 :
                    return "log/ins_notify";
                case 1 :
                    return "student/student_notify";
            }
            return "";
    }

    @PostMapping("/log_work")
    public String log_work(int userType){
        switch(userType){
            case 2 :
                return "log/ins_log_work";
            case 3 :
                return "log/log_work";
        }
        return "";
    }

    @PostMapping("/course_work")
    public String course_work(int userType){
        switch(userType){
            case 2 :
                return "log/ins_course_work";
            case 3 :
                return "log/course_work";
        }
        return "";
    }
}
